package student.action;

import java.io.File;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.util.MyLog;
import com.util.AU1;
//import com.util.Validation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

public class AdminRegisterUser extends ActionSupport implements
ServletRequestAware {

	private static final long serialVersionUID = 1L;
	private String commandButton = "";
	private String rollNor="";
	private String username="";
	private String password="";
	private String dob="";
	private String emailId="";
	private String image;
	private String msg="";
	private String errEmailid = "";
	
	public String getCommandButton() {
		return commandButton;
	}
	
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
	public String getRollNor() {
		return rollNor;
	}
	
	public void setRollNor(String rollNor) {
		this.rollNor = rollNor;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getDob() {
		return dob;
	}
	
	public void setDob(String dob) {
		this.dob = dob;
	}
	
	public String getEmailid() {
		return emailId;
	}
	
	public void setEmailid(String emailid) {
		this.emailId = emailid;
	}
	 public String getImage() {
			return image;
		}

		public void setImage(String image) {
			this.image = image;
		}
		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		//-----------------------------------------------------------------------------

	private File userImage;
	    private String userImageContentType;
	    private String userImageFileName;
	 
	    private HttpServletRequest servletRequest;
	    public File getUserImage() {
	        return userImage;
	    }
	 
	//    public void setUserImage(File userImage) {
	  //      this.userImage = userImage;
	  //  }
	 
	    public String getUserImageContentType() {
	        return userImageContentType;
	    }
	 
	    public void setUserImageContentType(String userImageContentType) {
	        this.userImageContentType = userImageContentType;
	    }
	 
	    public String getUserImageFileName() {
	        return userImageFileName;
	    }
	 
	    public void setUserImageFileName(String userImageFileName) {
	        this.userImageFileName = userImageFileName;
	    }
	 
	    public void setServletRequest(HttpServletRequest servletRequest) {
	        this.servletRequest = servletRequest;
	 
	    }
	 
	
//------------------------------------------------------------------------------
	
	public String execute() {
		if(this.commandButton.startsWith("Upload"))
		{
		          try {
		            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
		            System.out.println("Server path:" + filePath);
		            File fileToCreate = new File(filePath, this.userImageFileName);
		 
		            FileUtils.copyFile(this.userImage, fileToCreate);
		        } catch (Exception e) {
		            e.printStackTrace();
		            addActionError(e.getMessage());
		 
		           return "upload";
		        }
		          
		}  
		    
		 
		else{

		System.out.println("inside execute query"+rollNor+","+username+","+password+","+dob+","+emailId);		
	      	int result=insert(rollNor,username,password,dob,emailId);
       		 if(result>0){
       			 msg="User "+username +" registered successfully ";
       			 MyLog.log("registered user "+username +" successfully");
       		 }
       		 else{
       			msg="Cannot register user "+username + "";
       			 MyLog.log("Cannot register user "+username +"");
       			 return "error";
       		 }
		}
  	 	
  	 return "registerSuccess";
        }		
	
	
	public  int insert( String rollNor,String username,String password,String dob,String emailId) {
		System.out.println("inside insert query");
		ResultSet resultSet = null;
				
		String insertquery ="insert into user"
				+ " (roll_No,user_name,password,dob,email_id,image) " + "values('" +rollNor
				+ "', " + "'"+username+"',"+"'"+password +"',"+ "'"+dob +"',"+"'"+emailId +"',"+"'"+image+"' '');";
	
	//	Connection con= DB.getConnection();
		// Statement stm = null;
		 //try {
			//	stm = con.createStatement();
				//resultSet = stm.executeQuery(insertquery);
		//	}catch (SQLException e) {
			//	MyLog.log("Error while checking credentials from database"+e);
				
		//	}
		 return DB.update(insertquery);

	    }
	}
	
	